<template>
      <el-row class="warp">
        <el-col class="publicNav" :span="3" style="height:100%;">
            <router-link :to="{path:'/layout/alarm'}">告警参数设置</router-link>
            <router-link :to="{path:'/layout/contacts'}">联系人设置</router-link>
            <router-link class="newnava" :to="{path:'/layout/alarmtest'}">用户测试</router-link>
        </el-col>
        <el-col :span="21" style="height:100%;">
            <div class="alarmtest-box">
                <ul>
                  <li>
                      <div>测试方式</div>
                      <div>联系方式</div>
                      <div>测试指令</div>
                  </li>
                  <li>
                      <div >固话测试:</div>
                      <div>
                          <input type="text" v-model="telValue" name="" placeholder="18031111111">
                      </div>
                      <div>
                        <el-button type="primary" size="mini" @click="testFixing">测试</el-button>
                      </div>
                  </li>
                  <li>
                      <div >手机测试:</div>
                      <div>
                          <input v-model='phoneValue' placeholder="18031111111" type="text" name="">
                      </div>
                      <div>
                        <el-button type="primary" size="mini" @click="testPhone">测试</el-button>
                      </div>
                  </li>
                  <li>
                      <div>短信测试:</div>
                      <div>
                          <input v-model="messageValue" placeholder="18031111111" type="text" name="">
                      </div>
                      <div>
                        <el-button type="primary" size="mini" @click="testMessage">测试</el-button>
                      </div>
                  </li>
                </ul>
            </div>
        </el-col>
          <div v-if="isShow">
            <usertest :is-show="isShow" @on-close="onClose" :title="title" :active="active" :statusa="statusa" :result="result" :loading="loading2"></usertest>
          </div>
      </el-row>
</template>
<script>
import usertest from "./Monitornr/tem/usertest"
export default {
  components:{
      usertest
  },
  name: 'alarmtest',
  data () {
    return {
        tableData:[
          {
            name:'用户固话测试',
            btn:'test1'
          },
          {
            name:'用户手机测试',
            btn:'test2'
          },
          {
            name:'用户短信测试',
            btn:'test3'
          }
        ],
        isShow:false,
        title:"手机测试",
        telValue:"",
        phoneValue:"",
        messageValue:"",
        active:1,
        statusa:false,
        result:'测试中',
        loading2:true,
    }
  },
  methods: {
    get_pwd(){
        let  pwd; 
        $.ajax({
            url:'../cfg/password.json',
            type:'GET',
            cache:false,
            async: false,
            dataType:'text',
            success:function(result){
                let data = eval("("+result+")");
                pwd = data.password;
            },
            error:function(realdata){                           
            }
        })
        return pwd;
    },
    check_pwd(){
        let username = window.sessionStorage.getItem('username');
        let password = window.sessionStorage.getItem('password');
        let pwd = this.get_pwd();
        if(username == "admin"){
            if(password != pwd){
                this.$message({
                    message: '密码做了更改,请重新登录!',
                    type: 'info',
                    center:true,
                    showClose: true
                });
                this.loginout();
            }
        } 
    },
    loginout(){
        window.sessionStorage.removeItem("username");
        window.sessionStorage.removeItem("password");
       
        this.set_log("用户退出");
        this.$router.push("/login");
    },
    set_log(str){//用户操作日志
        let  msg= {"cmd":"CGI_LOG_SET_OPT","content":str};
        $.ajax({
            url:"cgi-bin/main.cgi",
            contentType: "application/json;charset=utf-8",
            type:"post",
            data:JSON.stringify(msg),
            success:function(msg){           
            }
        })
    },
      testFixing(){
          //let testing=/0\d{2}\d{7,8}/;
          let testing = /^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/;
          let testing1=/^[1][3,4,5,7,8][0-9]{9}$/;
          if(testing.test(this.$data.telValue)||testing1.test(this.$data.telValue)){
              let that=this;
              this.$data.isShow=true;
              this.$data.title="固话测试"
              let msg = {"cmd":"CGI_ALARM_TESTTELGH","number":this.$data.telValue};
              this.$ajax.post("./cgi-bin/main.cgi",msg).then(function(res){

                  let number = 0;
                  let tests = 0; 
                  let abce;

                    if(res.data.response==1){
                      setTimeout(function(){
                         abce=setInterval(function(){
                           get_fix_status()
                       },1000);
                      },5000)
                   }else{
                      that.$data.active=2;
                      that.$data.result="测试失败!";
                      that.$data.statusa=true;
                      that.$data.loading2=false;
                   }

                    function get_fix_status(){
                        that.$ajax.post("./cgi-bin/main.cgi",{"cmd":"CGI_ALARM_GET_FIXPHONETEST_STATUS"}).then(function(res){

                          switch(res.data.response){
                            case 0 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.result="测试成功";
                              that.$data.statusa=false;
                              clearInterval(abce);
                              break;
                            case 1 :
                              if(number<3&&tests!=0){
                                number=number+1;
                                that.$data.loading2=true;
                                that.$data.active=2;
                                that.$data.statusa=true;
                                that.$data.result="模块故障";
                                setTimeout(abce,1000);
                              }else{
                                clearInterval(abce);
                                that.$data.loading2=false;
                                that.$data.active=2;
                                that.$data.result="测试失败";
                                that.$data.statusa=true;
                               
                              }
                              break;
                            case 2 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="未接入电话线";
                              clearInterval(abce);
                              break;
                            case 4 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="无人接听";
                              clearInterval(abce);
                              break;
                            case 5 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="拨号失败";
                              clearInterval(abce);
                              break;
                            case 6 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="测试失败";
                              clearInterval(abce);
                              break;
                            case 7 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="网络故障";
                              clearInterval(abce);
                              break;
                            case 8 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="模块忙";
                              clearInterval(abce);
                              break;
                            case 9 :
                              that.$data.active=2;
                              that.$data.statusa=false;
                              that.$data.result="拨号中";
                              tests=9;
                              that.$data.loading2=true;
                              break;
                            case 10 :
                              that.$data.active=2;
                              that.$data.statusa=false;
                              that.$data.result="等待接通";
                               tests=10;
                              that.$data.loading2=true;
                              break;
                            case 11 :
                              that.$data.active=2;
                              that.$data.statusa=false;
                              tests=11;
                              that.$data.result="正在通话中";
                              that.$data.loading2=true;
                              break;
                         }
                        }).catch(function(){
                          
                        })
                    }
                    // if(res.data.response==0){
                    //   that.$data.active=2;
                    //   that.$data.result="测试成功";
                    //   that.$data.loading2=false;
                    // }else if(res.data.response==1){
                    //   that.$data.active=2;
                    //   that.$data.statusa=true;
                    //   that.$data.result="用户不存在";
                    //   that.$data.loading2=false;
                    // }else if(res.data.response==3){
                    //   that.$data.active=2;
                    //   that.$data.statusa=true;
                    //   that.$data.result="用户忙";
                    //   that.$data.loading2=false;
                    // }
                })
          }else{
              let that=this;
              this.$confirm('输入错误, 请重新填写！', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              }).then(() => {
                that.$data.telValue=''
              }).catch(() => {
                  that.$data.telValue=''
              });
          }
      },
      /*-1:函数执行失败；0：成功；1模块不存在；2：模块执行错误;3：模块忙*/
      testPhone(){
          let testing=/^[1][3,4,5,7,8][0-9]{9}$/;
          if(testing.test(this.$data.phoneValue)){
              let that=this;
              let msg = {"cmd":"CGI_ALARM_TESTTELSIM","number":this.$data.phoneValue};
              that.$data.title="手机测试";
              that.$data.isShow=true;
              // this.$ajax.post("./cgi-bin/main.cgi",msg).then(function(res){
              this.$ajax.post("./cgi-bin/main.cgi",msg).then(function(res){
                  let number = 0;
                  let tests = 0; 
                  let abce;
                    if(res.data.response==1){
                      abce=setInterval(function(){
                      get_phone_status()
                    },1000);
                   }else{
                      that.$data.loading2=false;
                      that.$data.active=2;
                      that.$data.result="测试失败!";
                      that.$data.statusa=true;
                   }
                    function get_phone_status(){
                        that.$ajax.post("./cgi-bin/main.cgi",{"cmd":"CGI_ALARM_GET_GSMPHONETEST_STATUS"}).then(function(res){
                          switch(res.data.response){
                            case 0 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.result="测试成功";
                              that.$data.statusa=false;
                              clearInterval(abce);
                              break;
                            case 1 :
                              if(number<3&&tests!=0){
                                number=number+1;
                                that.$data.loading2=true;
                                that.$data.active=2;
                                that.$data.statusa=true;
                                that.$data.result="模块故障";
                                setTimeout(abce,1000);
                              }else{
                                clearInterval(abce);
                                that.$data.loading2=false;
                                that.$data.active=2;
                                that.$data.result="模块故障";
                                that.$data.statusa=true;
                              }
                              break;
                            case 2 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="未插入SIM";
                              clearInterval(abce);
                              break;
                            case 3 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="无信号";
                              clearInterval(abce);
                              break;
                            case 5 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="无人接听";
                              clearInterval(abce);
                              break;
                            case 6 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="空号";
                              clearInterval(abce);
                              break;
                            case 7 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="网络故障";
                              clearInterval(abce);
                              break;
                            case 8 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="模块忙";
                              clearInterval(abce);
                              break;
                            case 9 :
                              that.$data.active=2;
                              that.$data.statusa=false;
                              that.$data.result="拨号中";
                              tests=9;
                              that.$data.loading2=true;
                              break;
                            case 10 :
                              that.$data.active=2;
                              that.$data.statusa=false;
                              that.$data.result="等待接通";
                               tests=10;
                              that.$data.loading2=true;
                              break;
                            case 11 :
                              that.$data.active=2;
                              that.$data.statusa=false;
                              tests=11;
                              that.$data.result="正在通话中";
                              that.$data.loading2=true;
                              break;
                         }
                        }).catch(function(){
                          
                        })
                    }
                })
          }else{
              let that=this;
              this.$confirm('输入错误, 请重新填写！', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              }).then(() => {
                that.$data.phoneValue='';
              }).catch(() => {
                  that.$data.phoneValue='';
              });
          }
      },
      testMessage(){
          let testing=/^[1][3,4,5,7,8][0-9]{9}$/;
          if(testing.test(this.$data.messageValue)){
              let that=this;
              that.$data.isShow=true;
              that.$data.title="短信测试";
               let msg = {"cmd":"CGI_ALARM_TESTMSGSIM","number":this.$data.messageValue};
              this.$ajax.post("./cgi-bin/main.cgi",msg).then(function(res){
                  let number = 0;
                  let tests = 0; 
                   let abce;
                  if(res.data.response==1){
                    abce=setInterval(function(){
                      get_message_status()
                    },1000);
                   }else{
                      that.$data.loading2=false;
                      that.$data.active=2;
                      that.$data.result="测试失败!";
                      that.$data.statusa=true;
                   }
                    function get_message_status(){
                        that.$ajax.post("./cgi-bin/main.cgi",{"cmd":"CGI_ALARM_GET_SMSTEST_STATUS"}).then(function(res){
                         
                          switch(res.data.response){
                            case 0 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.result="测试成功";
                              that.$data.statusa=false;
                              clearInterval(abce);
                              break;
                            case 1 :
                              if(number<3&&tests!=0){
                              
                                number=number+1;
                                that.$data.loading2=true;
                                that.$data.active=2;
                                that.$data.statusa=true;
                                that.$data.result="模块故障,再次尝试发送";
                                setTimeout(abce,1000);
                              }else{
                                that.$data.loading2=false;
                                that.$data.active=2;
                                that.$data.statusa=true;
                                that.$data.result="模块故障";
                                clearInterval(abce);
                              }
                              break;
                            case 2 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="未插入SIM";
                              clearInterval(abce);
                              break;
                            case 3 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="无信号";
                              clearInterval(abce);
                              break;
                            case 7 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="网络故障";
                              clearInterval(abce);
                              break;
                            case 8 :
                              that.$data.loading2=false;
                              that.$data.active=2;
                              that.$data.statusa=true;
                              that.$data.result="模块忙";
                              clearInterval(abce);
                              break;
                            case 11 :
                              that.$data.loading2=true;
                              that.$data.active=2;
                              that.$data.statusa=false;
                              tests=11;
                              that.$data.result="正在发送中";
                              break;
                         }
                        }).catch(function(){
                             that.$data.loading2=false;
                             that.$data.active=2;
                             that.$data.result="测试失败!";
                             that.$data.statusa=true;
                             clearInterval(abce);
                        })
                    }
                    
                })
          }else{
              let that=this;
              this.$confirm('输入错误, 请重新填写！', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              }).then(() => {
                that.$data.messageValue='';
              }).catch(() => {
                  that.$data.messageValue='';
              });
          }
      },
      onClose(){
          //this.$data.isShow=false;
          this.clearall();
          //this.$data.statusa=false;
      },
      clearall(){
        this.$data.statusa=false;
        this.$data.active=1;
        this.$data.result="测试中";
        this.$data.loading2=true;
        this.$data.isShow=false;
        this.$data.telValue="";
        this.$data.phoneValue="";
        this.$data.messageValue="";
      }
    },
    created(){
      this.check_pwd();
    }
}
</script>
<style >
.alarmwarp{
  width: 80%;
  margin: auto;
  margin-top: 5%;
}
.alarmwarp .el-radio+.el-radio{
  margin: 0px;
}
.humiturebtn{
  width: 80%;
  margin: auto;
  height: 30px;
  padding-top: 20px;
  overflow: hidden;
}
.humiturebtn .el-button{
  float: right;
  width: 80px;
}
.alarmcontent{
  min-height: 250px;
  width: 100%;
}
.alarmtest-box {
  width: 70%;
  margin:auto;
  margin-top: 10%;
  border: 1px solid #ebeef5;
  border-bottom: 0px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 1px 1px 15px #e6e6e6;
}
.alarmtest-box ul{
  list-style: none;
  outline: none;
}
.alarmtest-box ul li{
  width: 100%;
  height: 50px;
  line-height: 50px;
  text-align: center;
  border-bottom: 1px solid #ebeef5;
  padding: 8px;
}
.alarmtest-box ul li:hover{
  background: #f5f7fa;
}
.alarmtest-box ul li div{
  float: left;
  width: 33%;
  text-align: center;
  color: #606266;
  font-size: 14px;
}
.alarmtest-box ul li div input{
  border:1px solid #dcdfe6;
  height: 28px;
  width: 145px;
  margin: auto;
  border-radius: 4px;
  color:#606266;
  font-size: 14px;
  padding-left: 5px;
  line-height: 28px;
  text-align: center;
}
</style>
